package com.ruitu.mall_98_1.product.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.beanu.arad.utils.KeyboardUtils;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.mvp.contract.ProListByModelContract;
import com.ruitu.mall_98_1.mvp.model.ProListByModelModelImpl;
import com.ruitu.mall_98_1.mvp.presenter.ProListByModelPresenterImpl;
import com.ruitu.mall_98_1.product.adapter.ProductListAdapter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.TextWatcherImpl;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.product.ProList;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.utils.RcvUtils;

import java.util.List;

public class ProductListByModelActivity extends BaseProListActivity
        <ProListByModelPresenterImpl, ProListByModelModelImpl> implements ProListByModelContract.View {
    private LinearLayout ll_search;

    private String type = "";//模块类型 2 淘一淘 3 逛一逛 4 吃货街 5 厂家清仓 6 地方特产 7 行业精品 8 推荐商品 9 家居小世界
    private String model_name = "";//新的20个模块的名称

    private String keys = "";//搜索的关键字

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_product_list_by_model;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        et_keys = findViewById(R.id.et_keys);
        ll_search = findViewById(R.id.ll_search);

        setListeners();

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            type = bundle.getString("type");
            model_name = bundle.getString("model_name");
            setHeadTitle(getModelName(type));
        }
        reqFirst();
        showProgress();
    }

    private void setListeners() {
        tv_search.setOnClickListener(this);
        et_keys.addTextChangedListener(new TextWatcherImpl() {
            @Override
            public void afterTextChanged(Editable s) {
                keys = et_keys.getText().toString().trim();
                if (TextUtils.isEmpty(keys)) {//输入的关键字为空
                    tv_search.setText("取消");
                } else {
                    tv_search.setText("搜索");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_search) {//点击右上角的搜索/取消
            if (TextUtils.equals("取消", tv_search.getText().toString())) {//右上角显示的是取消
                tv_search.setText("搜索");
                showSearchView(false);
                KeyboardUtils.hideSoftInput(et_keys);
            } else {//右上角显示的搜索
                if (ll_search.getVisibility() == View.VISIBLE) {//说明已经打开了搜索框
                    hideProgress();
                    showProgress();
                    currPage = 1;
                    productList.clear();
                    mPresenter.reqProList(type, CITY_NAME, keys, currPage, 20);
                } else {//先打开搜索框
                    showSearchView(true);
                }
            }
        }
    }

    private void showSearchView(boolean isShow) {
        if (isShow) {
            ll_search.setVisibility(View.VISIBLE);
        } else {
            ll_search.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onItemClick(View v, int pos) {
        Bundle bundle = new Bundle();
        Product p = productList.get(pos);
        bundle.putSerializable("product", p);
        startActivity(ProductDetailActivity.class, bundle);
    }

    private String CITY_NAME = AppHolder.instance().getCITY().getName();

    @Override
    protected void reqFirst() {
        currPage = 1;
        productList.clear();
        mPresenter.reqProList(type, CITY_NAME, "", currPage, 20);

        RcvUtils.refreshCompleteDelay(lRecyclerView, null);
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            mPresenter.reqProList(type, CITY_NAME, keys, currPage, 20);
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    public void onReqComlete(boolean isSuc, int type, Object data) {
        if (type == 1) {//商品列表请求完成
            PageModel<ProList> pageData = (PageModel<ProList>) data;
            currPage = pageData.getCurrentPage();
            totalPage = pageData.getTotalPage();
            totalNum = pageData.getTotal();
            List<ProList> newList = pageData.getDataList();
            if (!isDataEmpty(newList)) {
                for (int i = 0; i < newList.size(); i++) {
                    productList.add(newList.get(i).getGood());

                }
            }
            productAdapter.setDataList(productList);
        }
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        productAdapter = new ProductListAdapter(this);
        productAdapter.type = 2;
        return productAdapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.l_rcv_product);
    }

    private String getModelName(String type) {
        if (!TextUtils.isEmpty(model_name)) {
            return model_name;
        }
        if (TextUtils.equals("2", type)) {
            return "淘一淘";
        } else if (TextUtils.equals("3", type)) {
            return "逛一逛";
        } else if (TextUtils.equals("4", type)) {
            return "吃货街";
        } else if (TextUtils.equals("5", type)) {
            return "厂家清仓";
        } else if (TextUtils.equals("6", type)) {
            return "地方特产";
        } else if (TextUtils.equals("7", type)) {
            return "行业精品";
        } else if (TextUtils.equals("8", type)) {
            return "推荐商品";
        } else if (TextUtils.equals("9", type)) {
            return "家居世界";
        }
        return "九八商城";
    }
}
